﻿using MediatR;
using SheepStorage.API.Application.Command.BuildingManagementCommand;
using SheepStorage.API.Application.Command.SheepWeightCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.DataImp;
using SheepStorage.Service.Interface;

namespace SheepStorage.API.Application.CommandHandlers.SheepWeightHandler
{
    public class SheepWeightCreateHandler : IRequestHandler<SheepWeightCreateCommand, Results<int>>
    {

        private readonly ISheepWeightService service;
        private readonly ISheepRecordService sheepRecordService;

        public SheepWeightCreateHandler(ISheepWeightService service, ISheepRecordService sheepRecordService)
        {
            this.service = service;
            this.sheepRecordService = sheepRecordService;
        }

       public async Task<Results<int>> Handle(SheepWeightCreateCommand request, CancellationToken cancellationToken)
        {
            var entity = new SheepWeight
            {
                SheepWeightId = request.SheepWeightId,
                OverBit = request.OverBit,
                WeightMonth=request.WeightMonth,
                SheepSex=request.SheepSex==1?true:false,
                WeightType = request.WeightType,
                SheeHeavy = request.SheeHeavy,
             
                WeightDate = request.WeightDate,
                AddTime = DateTime.Now,
                AddName = "侯佳",
                IsDel = 0,

            };

            //var list = await service.AddSheepWeight(entity);
            //return Results<int>.DataResult(list);

            await service.AddSheepWeight(entity);
            var list = await sheepRecordService.GetSheepRecordAll();
            var result = list.FirstOrDefault(c => c.OverBit == entity.OverBit);
            if (result != null)
            {
                switch (entity.WeightType)
                {
                    case "断奶重":

                        result.SheepRecordWeaningWeight = entity.SheeHeavy;
                        break;
                    default:
                        result.SheepRecordWeightBirth = entity.SheeHeavy;
                        break;


                }
                await sheepRecordService.UpdateSheepRecord(result);

            }
            return Results<int>.DataResult(1);


            //return Results<int>.DataResult(list);
        }
    }
}
